<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 左侧 -->
          <el-tab-pane label="角色管理">
            <!-- 按钮 -->
            <el-button icon="el-icon-plus" size="small" type="primary" @click="openAddDig">新增角色</el-button>
            <!-- 表格 -->
            <el-table :data="roleList" v-loading="loading">
              <el-table-column label="序号" width="100" type="index" :index="indexMethod" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{ row }">
                  <el-button size="small" type="success" @click="clickShowAssignDialog(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRole(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>

          <el-tab-pane label="公司信息">
            <!-- 警告信息 -->
            <el-alert title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改" type="info" show-icon :closable="false" />
            <!-- 表单 -->
            <el-form label-width="120px" style="margin-top:50px" :model="companyForm">
              <el-form-item label="公司名称">
                <el-input disabled style="width:400px" v-model="companyForm.name" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input disabled style="width:400px" v-model="companyForm.companyAddress" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input disabled style="width:400px" v-model="companyForm.mailbox" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input type="textarea" :rows="3" disabled style="width:400px" v-model="companyForm.remarks" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 分页 -->
    <el-pagination :current-page="params.page" :page-sizes="[5, 10, 20]" :page-size="params.pageSize"
      layout="->,total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
      @current-change="handleCurrentChange" />

    <!-- 添加权限弹窗 -->
    <AddRole :showDialog="showDialog" @close-dialog="showDialog = false" ref="addRole"></AddRole>

    <!-- 分配权限弹窗 -->
    <SetRole :showAssignDialog.sync="showAssignDialog" :userId="userId" ref="setRole"></SetRole>
  </div>
</template>

<script>
import { getRoleListAPI, delRoleAPI, getRoleDetailAPI } from '@/api/setting'
import { getCompanyByIdAPI } from '@/api/company'
import AddRole from './components/AddRole.vue'
import SetRole from './components/SetRole.vue'

export default {
  name: 'Setting',
  components: {
    AddRole,
    SetRole
  },
  data() {
    return {
      params: {
        page: 1,
        pagesize: 10
      },
      roleList: [],  // 角色列表
      total: 0, // 总条数
      loading: false,  // 动态设置 loading 的状态
      showDialog: false, // 弹框显示隐藏状态
      // 公司信息
      companyForm: {
        name: '',
        companyAddress: '',
        mailbox: '',
        remarks: ''
      },
      showAssignDialog: false,  // 控制弹层的显示隐藏
      userId:  ''
    }
  },
  created() {
    this.getRoleList()
    this.getCompanyById()
  },
  methods: {
    // 打开分配权限
    async clickShowAssignDialog(id) {
      this.showAssignDialog = true
      this.userId = id
      this.$refs.setRole.getPermissionDetail(id)
    },
    // 获取企业信息
    async getCompanyById() {
      // 企业id在vuex中
      const res = await getCompanyByIdAPI(this.$store.state.user.userInfo.companyId)
      // console.log('企业信息', res.data)
      this.companyForm = res.data
    },
    // 编辑
    async editRole(id) {
      this.showDialog = true
      const res = await getRoleDetailAPI(id)
      this.$refs.addRole.form = res.data
    },
    // 打开添加模态框
    openAddDig() {
      this.showDialog = true
    },
    // 删除角色
    delRole(id) {
      this.$confirm('你确定要删除这个角色吗', '温馨提示').then(async () => {
        await delRoleAPI(id)
        if (this.roleList.length === 1 && this.params.page > 1) {
          this.page--
        }
        this.$message.success('删除成功')
        this.getRoleList()
      }).catch(err => { console.log(err) })
    },
    // 获取-角色列表
    async getRoleList() {
      this.loading = true
      const res = await getRoleListAPI(this.params)
      console.log('角色列表', res.data.rows)
      this.roleList = res.data.rows
      this.total = res.data.total
      this.loading = false
    },
    // 页码跳转
    handleCurrentChange(page) {
      this.params.page = page
      this.getRoleList()
    },
    // 页容量跳转
    handleSizeChange(pagesize) {
      this.params.page = 1
      this.params.pagesize = pagesize
      this.getRoleList()
    },
    // 处理序号
    indexMethod(index) {
      return (this.params.page - 1) * this.params.pagesize + index + 1
    }
  }
}
</script>
<style scoped lang="scss"></style>
